package org.hxyjs.controller;

import org.hxyjs.dto.DTO;
import org.hxyjs.entity.User;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@Controller
@RequestMapping("/userController")
public class UserController {
	@Resource
	UserService userService;
	@RequestMapping("/getAllUser")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<User> getAllUser() throws Exception {
    	System.out.println("查询全部用户");
		return userService.getAllUsers();
    }

	@RequestMapping("/deleteUserByUno/{uno}")
	@ResponseBody
    public DTO<User> deleteUserByUno(@PathVariable("uno") String uno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(uno+"删除用户");
    	return userService.deleteUserByUno(uno);
    }

	@RequestMapping("/updateUserByUno")
	@ResponseBody
	public DTO<User> updateUserByUno(@RequestBody User user) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改用户");
		return userService.updateUserByUno(user);
	}


	@RequestMapping("/addUser")
	@ResponseBody
	public DTO<User> addUser(@RequestBody User user) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加用户");
		System.out.println(user);
 		return userService.addUser(user);
	}
	@RequestMapping("/getUserByPage")
	@ResponseBody
	public DTO<User> getUserByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("分页用户");
		String cacheId = "UserByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		System.out.println(cacheId);
		return pageInfo.getCurrPage() > 2 ? userService.getUserByPage(pageInfo) :
				userService.getUserByPage(pageInfo, cacheId);
	}

}